<?php
namespace app\admin\controller\agent;

use think\Db;
class Index extends \app\admin\controller\Common
{
    protected $type = 0;
    public function index(){
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\Agent());
        isset($search['title']) && $search['title'] != "" ? $query->where("title like '%{$search['title']}%'") : '';
        $query->order("id desc");
        $list = $query->limit($page, $limit)
                ->append(['level_title_text'])
                ->select();
        $count = $query->count();
   
        return self::successResult('',$list,$count);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            
            $validate = new \app\common\validate\Agent();
            $result = $validate->scene('add')->check($data);
            if ($result === false) {
                $this->error($validate->getError());
            }
            if(isset($data['password'])){
                $data['pass'] = StrMD5($data['password']);
            }
 
            $table = (new \app\common\model\Agent());
            $table->allowField(true)->save($data);
        
            if ($table->id) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $cate = (new \app\common\model\Classify())->lists();
            $this->assign("cate",$cate);
            $level = (new \app\common\model\Level())->select();
            $this->assign("level",$level);
            $province = (new \app\common\model\City())->where('LevelType', 1)->field(['id', 'Name'])->select();
            $this->assign("province", $province);
            return view();
        }
    }
    
    public function edit()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            $id = request()->param('id');
       
            $table = new \app\common\model\Agent();
            $row = $table->allowField(true)->save($data, ['id' => $id]);
            if ($row) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $id = intval(request()->param('id'));
            
            $info = (new \app\common\model\Agent())->where(['id' => $id])->find();
            $this->assign('info',$info);
            $cate = (new \app\common\model\Classify())->lists();
            $this->assign("cate",$cate);
            $level = (new \app\common\model\Level())->select();
            $this->assign("level",$level);
            $province = (new \app\common\model\City())->where('LevelType', 1)->field(['id', 'Name'])->select();
            $this->assign("province", $province);
            $city = (new \app\common\model\City())->where('ParentId', $info['province_id'])->field('id,Name')->select();
            $this->assign('city', $city);
            $area = (new \app\common\model\City())->where('ParentId', $info['city_id'])->field('id,Name')->select();
            $this->assign('area', $area);
   
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Agent();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }
    public function pass(){
        $id = request()->param('id');
        if (request()->isPost()) {
            $data = request()->param('info');
            // $validate = new \app\common\validate\Store();
            // $result = $validate->scene('pass')->check($data);
            // if ($result === false) {
            //     self::errorResult($validate->getError());
            // }
            $arr = [];
            if(!empty($data['password']) ){
                $arr['pass'] = StrMD5($data['password']);
            }
            if(!empty($data['usename'])){
                $arr['usename'] = $data['usename'];
            }
            if(isset($arr)){
                 (new \app\common\model\Agent())->where(['id' => $id])->update($arr);
            }

            return self::successResult();
        } else {
            $this->assign('info',(new \app\common\model\Agent())->where(['id' => $id])->find());
            $this->assign('id',$id);
            return view();
   
        }
  
    }
 
}
